//
//  ChooseVCView.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2016/12/28.
//  Copyright © 2016年 汪的一声就哭了！. All rights reserved.
//

#import "ChooseVCView.h"

@interface ChooseVCView ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSMutableArray *viewCArr;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, assign) NSInteger arrNum;
@property (nonatomic, strong) UIColor *defaultColor;
@property (nonatomic, strong) UIColor *selectedColor;
@property (nonatomic, assign) CGRect myTitleRect;
@property (nonatomic, assign) CGRect scrollViewFrame;
@property (nonatomic, strong) UIColor *scrollBackgroundColor;
@end

@implementation ChooseVCView
- (void)setDelegate:(id<ChooseVCViewDelegate>)delegate
{
    _delegate = delegate;
    [self addViewOnScrollView];
    [delegate chooseVCViewDidChoose:0 and:_viewCArr[0]];
}
- (void)setScrollViewFrame:(CGRect)scrollViewFrame
{
    _scrollViewFrame = scrollViewFrame;
    self.scrollView.frame = scrollViewFrame;
    self.scrollView.contentSize = CGSizeMake(scrollViewFrame.size.width*self.arrNum, 0);
}
- (void)setScrollBackgroundColor:(UIColor *)scrollBackgroundColor
{
    _scrollBackgroundColor = scrollBackgroundColor;
    self.scrollView.backgroundColor = _scrollBackgroundColor;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame btnArray:(NSArray<NSString *> *)btnArray font:(UIFont *)font textColor:(UIColor *)textColor selectedImg:(UIImage *)selectedImg aSelectedColor:(UIColor *)aSelectedColor titleRect:(CGRect)titleRect titleHeight:(CGFloat)titleHeight
{
    self = [super initWithFrame:frame];
    if (self) {
        self.chooseInt = 0;
        self.arrNum = btnArray.count;
        self.myTitleRect = titleRect;
        self.scrollViewFrame = CGRectMake(0, titleHeight, self.bounds.size.width, self.bounds.size.height-titleHeight);
        self.defaultColor = textColor;
        self.selectedColor = aSelectedColor;
        self.headView = [[UIScrollView alloc] initWithFrame:titleRect];
        if (btnArray.count <= 5) {
            _headView.contentSize = CGSizeMake(self.myTitleRect.size.width, 0);
        } else {
            _headView.contentSize = CGSizeMake(self.myTitleRect.size.width/5*_arrNum, 0);
        }
        self.imageView = [[UIImageView alloc] init];
        self.imageView.frame = CGRectMake(0, 0, self.headView.contentSize.width/self.arrNum, self.myTitleRect.size.height);
        [self.headView addSubview:self.imageView];
        [self addSubview:self.headView];
        self.imageView.image = selectedImg;
        self.imageView.userInteractionEnabled = YES;
        for (NSInteger i = 0; i < self.arrNum; i++) {
            NSString *nameStr = btnArray[i];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i*self.headView.contentSize.width/self.arrNum, 0, self.headView.contentSize.width/self.arrNum, titleRect.size.height)];
            [self.headView addSubview:label];
            label.text = nameStr;
            label.font = font;
            label.textAlignment = NSTextAlignmentCenter;
            label.backgroundColor = [UIColor clearColor];
            label.textColor = textColor;
            if (i == 0) {
                label.textColor = self.selectedColor;
            }
            label.userInteractionEnabled = YES;
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self.headView addGestureRecognizer:tap];
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.scrollViewFrame];
        self.scrollView.bounces = NO;
        self.scrollView.contentSize = CGSizeMake(self.scrollViewFrame.size.width*self.arrNum, 0);
        [self addSubview:self.scrollView];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
    }
    return self;
}
#pragma mark----------在代理方法添加视图
- (void)addViewOnScrollView
{
    self.viewCArr = [NSMutableArray array];
    for (NSInteger i = 0; i < self.arrNum; i++) {
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(i*self.scrollViewFrame.size.width, 0, self.scrollViewFrame.size.width, self.scrollViewFrame.size.height)];
        [self.scrollView addSubview:backView];
        UIViewController *viewC = [self.delegate chooseVCViewForSelectedAt:i];
        [self.viewCArr addObject:viewC];
        [backView addSubview:viewC.view];
    }
}
#pragma mark------------头视图点击方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSInteger inte = (NSInteger)[tap locationInView:self.headView].x/self.headView.contentSize.width*self.arrNum;
    self.chooseInt = inte;
    for (NSInteger i=1; i <= self.arrNum; i++) {
        UILabel *label = (UILabel *)self.headView.subviews[i];
        label.textColor = self.defaultColor;
    }
    UILabel *label = (UILabel *)self.headView.subviews[inte+1];
    label.textColor = self.selectedColor;
    [UIView animateWithDuration:0.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(inte*self.scrollViewFrame.size.width, 0);
        self.imageView.center = CGPointMake(self.headView.contentSize.width/self.arrNum*inte+self.headView.contentSize.width/self.arrNum/2, self.imageView.center.y);
    }];
}
#pragma mark-----------scrollview代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger inte = self.scrollView.contentOffset.x/self.scrollViewFrame.size.width;
    self.chooseInt = inte;
    if (self.arrNum > 5) {
        if (inte > 3) {
            self.headView.contentOffset = CGPointMake(self.headView.contentSize.width/self.arrNum*(inte-3), 0);
        }
    }
    CGFloat flo = self.scrollView.contentOffset.x/self.scrollViewFrame.size.width;
    if (inte == flo) {
        [self.delegate chooseVCViewDidChoose:inte and:self.viewCArr[inte]];
    }
    self.imageView.center = CGPointMake(self.headView.contentSize.width/self.arrNum*flo+self.headView.contentSize.width/self.arrNum/2, self.imageView.center.y);
    for (NSInteger i=1; i <= self.arrNum; i++) {
        UILabel *label = (UILabel *)self.headView.subviews[i];
        label.textColor = self.defaultColor;
    }
    UILabel *label = (UILabel *)self.headView.subviews[inte+1];
    label.textColor = self.selectedColor;
    if (flo > inte+0.5 && inte < self.arrNum-1) {
        UILabel *label = (UILabel *)self.headView.subviews[inte+1];
        label.textColor = self.defaultColor;
        UILabel *label1 = (UILabel *)self.headView.subviews[inte+2];
        label1.textColor = self.selectedColor;
    }
    if (flo < inte-0.5 && inte > 0) {
        UILabel *label = (UILabel *)self.headView.subviews[inte+1];
        label.textColor = self.defaultColor;
        UILabel *label1 = (UILabel *)self.headView.subviews[inte];
        label1.textColor = self.selectedColor;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
